﻿using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Windows.Forms;

public static class HTTPManager
{
    public static string SendMessage(string prefix, string msg, Dictionary<string, string> headers)
    {
        try
        {
            if (prefix.Contains("127.0.0.1")) prefix = prefix.Replace("127.0.0.1", "localhost");
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://" + prefix, UriKind.Absolute));
            req.Timeout = 10000;
            if (msg != null)
            {
                req.Method = "POST";
                StreamWriter writer = new StreamWriter(req.GetRequestStream());
                writer.Write(msg);
                if (headers != null)
                {
                    foreach (KeyValuePair<string, string> h in headers)
                    {
                        req.Headers.Add(h.Key, h.Value);
                    }
                }
                writer.Flush();
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string str = reader.ReadToEnd();
                writer.Close();
                return str;
            }
            else
            {
                req.Method = "GET";
                if (headers != null)
                {
                    foreach (KeyValuePair<string, string> h in headers)
                    {
                        req.Headers.Add(h.Key, h.Value);
                    }
                }
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string data = reader.ReadToEnd();
                return data;
            }
        }catch(Exception ex)
        {
            if(ex is WebException)
            {
                MessageBox.Show("Http connection has been timmed out! This might be a server exception or your connection has been aborted.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return "ERROR: SERVER CONNECTION TIMMED OUT!";
            }
            return "Unknown Error!";
        }
    }

    public static HttpWebResponse SendMessage3(string prefix, byte[] msg, Dictionary<string, string> headers)
    {
        try
        {
            if (prefix.Contains("127.0.0.1")) prefix = prefix.Replace("127.0.0.1", "localhost");
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://" + prefix, UriKind.Absolute));
            req.Timeout = 10000;
            if (msg != null)
            {
                req.Method = "POST";
                Stream writer = req.GetRequestStream();
                writer.Write(msg, 0, msg.Length);
                if (headers != null)
                {
                    foreach (KeyValuePair<string, string> h in headers)
                    {
                        req.Headers.Add(h.Key, h.Value);
                    }
                }
                writer.Flush();
                writer.Close();
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                return response;
            }
            else
            {
                req.Method = "GET";
                if (headers != null)
                {
                    foreach (KeyValuePair<string, string> h in headers)
                    {
                        req.Headers.Add(h.Key, h.Value);
                    }
                }
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                return response;
            }
        }
        catch (Exception ex)
        {
            if (ex is WebException)
            {
                MessageBox.Show("Http connection has been timmed out! This might be a server exception or your connection has been aborted.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            return null;
        }
    }

    public static HttpWebResponse SendMessage2(string prefix, string msg, Dictionary<string, string> headers)
    {
        try
        {
            if (prefix.Contains("127.0.0.1")) prefix = prefix.Replace("127.0.0.1", "localhost");
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://" + prefix, UriKind.Absolute));
            req.Timeout = 10000;
            if (msg != null)
            {
                req.Method = "POST";
                req.SendChunked = false;
                StreamWriter writer = new StreamWriter(req.GetRequestStream());
                writer.Write(msg);
                if (headers != null)
                {
                    foreach (KeyValuePair<string, string> h in headers)
                    {
                        req.Headers.Add(h.Key, h.Value);
                    }
                }
                writer.Flush();
                writer.Close();
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                return response;
            }
            else
            {
                req.Method = "GET";
                if (headers != null)
                {
                    foreach (KeyValuePair<string, string> h in headers)
                    {
                        req.Headers.Add(h.Key, h.Value);
                    }
                }
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                return response;
            }
        }catch(Exception ex)
        {
            HttpWebResponse res = null;
            if (ex is WebException)
            {
                MessageBox.Show("Http connection has been timmed out! This might be a server exception or your connection has been aborted.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return res;
            }
            return res;
        }
    }
}